﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text;

namespace Surging.Core.Common
{
    /// <summary>
    /// 用于服务端和客户端通讯的模型
    /// </summary>
    public class ResultDto
    {
        public ResultDto()
        { }
        public ResultDto(ResultCode code, string msg, object data)
        {
            Code = code;
            Msg = msg;
            Data = data;
        }

        public static ResultDto Error(string message)
        {
            return new ResultDto { Code = ResultCode.Fail, Data = null, Msg = message };
        }
        public static ResultDto Error(string message,object data)
        {
            return new ResultDto { Code = ResultCode.Fail, Data = data, Msg = message };
        }
        public static ResultDto Success(object data)
        {
            return new ResultDto { Code = ResultCode.Success, Data = data, Msg = "" };
        }
        public static ResultDto Success(string message, object data)
        {
            return new ResultDto { Code = ResultCode.Success, Data = data, Msg = message };
        }

        /// <summary>
        /// 返回标识：1表示成功，0表示失败，401表示未登录，402表示没权限
        /// </summary>
        public ResultCode Code { get; set; }

        /// <summary>
        /// 服务端返回的文本信息，一般当做服务端出错信息返回
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 服务端返回的数据
        /// </summary>
        public object Data { get; set; }
    }
    /// <summary>
    /// 用于服务端和客户端通讯的模型
    /// </summary>
    [DataContract]
    public class ResultDto<T>
    {
        
        public ResultDto()
        { }
       
        public ResultDto(ResultCode code, string msg, T data)
        {
            Code = code;
            Msg = msg;
            Data = data;
        }
        public static ResultDto<T> Error(string message)
        {
            return new ResultDto<T> { Code = ResultCode.Fail, Data = default(T), Msg = message };
        }
        public static ResultDto<T> Error(string message, T data)
        {
            return new ResultDto<T> { Code = ResultCode.Fail, Data = data, Msg = message };
        }
        public static ResultDto<T> Success(T data)
        {
            return new ResultDto<T> { Code = ResultCode.Success, Data = data, Msg = "" };
        }
        public static ResultDto<T> Success(string message, T data)
        {
            return new ResultDto<T> { Code = ResultCode.Success, Data = data, Msg = message };
        }
        /// <summary>
        /// 返回标识：1表示成功，0表示失败，401表示未登录，402表示没权限
        /// </summary>
        [DataMember]
        public ResultCode Code { get; set; }

        /// <summary>
        /// 服务端返回的文本信息，一般当做服务端出错信息返回
        /// </summary>
        [DataMember]
        public string Msg { get; set; }

        /// <summary>
        /// 服务端返回的数据
        /// </summary>
        [DataMember]
        public T Data { get; set; }
    }
    public enum ResultCode
    {
        /// <summary>
        /// 失败
        /// </summary>
        [Description("Fail")]
        Fail = 0,
        /// <summary>
        /// 成功
        /// </summary>
        [Description("Success")]
        Success = 1,
        /// <summary>
        /// 未授权
        /// </summary>
        [Description("UnAuthorized")]
        UnAuthorized = 401
    }

}
